﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bacteria
{
    /// <summary>
    /// Interaction logic for pageFermentation.xaml
    /// </summary>
    public partial class pageFermentation : Page
    {
        MainWindow main;
        MainWindow.Group group;
        bool SkipToggleSwitchEvent = false;
        Label[] TestLabel;
        ToggleSwitch.HorizontalToggleSwitch[] TestToggle;
        public pageFermentation(MainWindow m)
        {
            main = m;
            InitializeComponent();
            
            TestLabel = new Label[] { Test1Label, Test2Label, Test3Label };
            TestToggle = new ToggleSwitch.HorizontalToggleSwitch[] { Test1Toggle, Test2Toggle, Test3Toggle };

            bacteriaList.Text = "";
            FinalResultLabel.Content = "";
            foreach (var l in TestLabel) l.Visibility = Visibility.Hidden;
            foreach (var t in TestToggle) t.Visibility = Visibility.Hidden;
        }

        private void cmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = 0;
            FinalResultLabel.Content = "";

            var s = (ComboBox)sender;
            if (s == cmbLactose) main.gLactose = (MainWindow.Fermentation)s.SelectedIndex;
            if (s == cmbDextrose) main.gDextrose = (MainWindow.Fermentation)s.SelectedIndex;
            if (s == cmbSucrose) main.gSucrose = (MainWindow.Fermentation)s.SelectedIndex;

            if (cmbLactose.SelectedIndex >= 0 && cmbDextrose.SelectedIndex >= 0 && cmbSucrose.SelectedIndex >= 0)
            {
                group = main.FindGroup(main.gGramTest, main.gCellShape, main.gLactose, main.gDextrose, main.gSucrose);
                bacteriaList.Text = "Possible Organisms:\n\n";
                if (group.Number != MainWindow.GroupNum.None)
                {
                    // Display list of bacteria
                    foreach (var bacterium in group.Bacteria)
                        bacteriaList.Text += bacterium.ToString().Replace("_", ". ") + "\n";

                    // Display list of tests
                    foreach (var test in group.Tests)
                    {
                        TestLabel[i].Content = test.ToString().Replace('_', ' ') + ":";
                        TestLabel[i].Visibility = Visibility.Visible;
                        TestToggle[i].Visibility = Visibility.Visible;

                        i++;
                    }
                }
                else
                {
                    bacteriaList.Text = "";
                    foreach (var l in TestLabel) l.Visibility = Visibility.Hidden;
                    SkipToggleSwitchEvent = true;
                    foreach (var t in TestToggle) { t.Visibility = Visibility.Hidden; t.IsChecked = false; }
                    SkipToggleSwitchEvent = false;
                }
            }
        }

        private void TestxToggle_CheckedChanged(object sender, RoutedEventArgs e)
        {
            var TestList = new SortedList<MainWindow.Tests, MainWindow.TestResult>();

            if (SkipToggleSwitchEvent) return;

            for (int i = 0; i < group.Tests.Count; i++)
                TestList.Add(group.Tests[i], (TestToggle[i].IsChecked ? MainWindow.TestResult.Positive : MainWindow.TestResult.Negative));
            
            var bacteria = main.FindBacteria(group, TestList);
            bacteriaList.Text = "";
            FinalResultLabel.Content = bacteria.ToString().Replace("_", ". ");
        }
    }
}
